package com.example.holes;

import java.util.ArrayList;

import android.graphics.Bitmap;
import android.graphics.Canvas;

public abstract class Mole {
	protected final int MAX_FRAMES = 5;
	protected final int TIME_BETWEEN = 2;
	private final int lowerTime = MAX_FRAMES * TIME_BETWEEN;
	protected Bitmap image = null, orig = null;
	protected Bitmap[] mImages;
	protected float left, top, topInc;
	protected int holeNum, row, height, ycutoff;
	protected int health, lifespan, attack, points;
	protected boolean start = false;
	private boolean rising = false, lowering = false;
	protected int animateTimer = 1;
	protected int frame = 0;

	public Mole(int holeNum, ArrayList<Bitmap> images, int imageIndex){
		this.holeNum = holeNum;
		
		setImagePosition(holeNum);
		setImage(images, imageIndex);
		height = orig.getHeight();
		this.topInc = calcInc();
		
		// scale position for different screen sizes
		topInc = Global.SCALE_RATIO_H * topInc;
		ycutoff = (int)(Global.SCALE_RATIO_H * ycutoff);
		left = (int)(Global.SCALE_RATIO_W * left);
		top = (int)(Global.SCALE_RATIO_H * top);
	}
	
	public abstract void setImage(ArrayList<Bitmap> images, int imageIndex);
	public abstract int timeUp();
	public abstract int attack(int attack);
	public abstract void setImagePosition(int holeNum);
	
	// set the ycutoff (where image enters hole)
	// and then return the yInc to get there in MAX_FRAMES
	private float calcInc(){
		switch(row){
			case 1: ycutoff = 419; break;
			case 2: ycutoff = 287; break;
			case 3: ycutoff = 188; break;
		}
		
		return (ycutoff - top) / MAX_FRAMES;
	}
	
	// ===============
	//    v UPDATE v
	// ===============
	public void Update(){
		if (start){ // start is set by Hole only
			lifespan--;
			lifespanimations();
		}
	}
	
	// handles queueing the rising and lowering animations
	protected void lifespanimations(){
		if (lifespan == lowerTime)
			lowering = true;
		
		if (rising)
			risingAnimation();
		
		else if (lowering)
			loweringAnimation();
	}
	// ===============
	//    ^ UPDATE ^
	// ===============

	// ==============
	//     v DRAW v
	// ==============
	public void Draw(Canvas c){
		if (image != null){
			c.drawBitmap(image, left, top, null);
		}
	}

	// animates mole rising
	protected void risingAnimation() {
		animateTimer--;
		if (animateTimer == 0){
			frame++;
			animateTimer = TIME_BETWEEN;
			if (frame == MAX_FRAMES){
				rising = false;
			}
			else{
				nextFrame();
			}
		}
	}

	// animates mole lowering
	protected void loweringAnimation(){
		animateTimer--;
		if (animateTimer == 0){
			frame--;
			animateTimer = TIME_BETWEEN;
			if (frame == 0){
				lowering = false;
			}
			else{
				nextFrame();
			}
		}
	}
	
	protected void nextFrame(){
		if (rising)
			top -= topInc;
		else if (lowering)
			top += topInc;
		int topheight = (int)(top + height);
		// crop image
		if ((topheight) > ycutoff){
			image = Bitmap.createBitmap(orig, 0, 0, orig.getWidth(), height - (topheight - ycutoff));
		}
		else{
			image = orig;
		}
	}
	// =============
	//   ^  DRAW ^
	// =============
	
	// getters
	public int getHole(){ return holeNum; }
	public int getHealth(){ return health; }
	public int getAttack(){ return attack; }
	public int getPoints(){ return points; }
	
	// setters
	public void start(){
		top += (int)((MAX_FRAMES - 1) * topInc);
		start = true;
		rising = true;
	}
}
